/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.config.autoconfigure;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.tianyun.cloud.config.console.ConsoleBeanPostProcessor;
import org.tianyun.cloud.config.console.ConsolePropertySourceLocator;
import org.tianyun.cloud.config.console.ConsolePushConfigListenner;
import org.tianyun.cloud.config.console.configuration.ConsoleConfigProperties;
import org.tianyun.cloud.config.refresh.smart.EnvironmentChangeEventListenner;
import org.tianyun.cloud.config.refresh.smart.SmartRefresher;
import org.tianyun.cloud.config.refresh.smart.SmartScope;

/**
 * 配置中心自动配置类
 *
 * @auther ebert_chan
 */
@Configuration(proxyBeanMethods = false)
public class CloudConfigAutoConfiguration {

    @Bean
    @ConditionalOnBean(ConsolePropertySourceLocator.class)
    public ConsoleBeanPostProcessor consoleBeanPostProcessor() {
        return new ConsoleBeanPostProcessor();
    }

    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    @ConditionalOnMissingBean(SmartScope.class)
    public SmartScope smartScope() {
        return new SmartScope();
    }

    @Bean
    public SmartRefresher smartRefresher(SmartScope smartScope) {
        return new SmartRefresher(smartScope);
    }

    @Bean
    public EnvironmentChangeEventListenner smartRefreshListenner(SmartRefresher smartRefresher) {
        return new EnvironmentChangeEventListenner(smartRefresher);
    }

    @Bean
    @ConditionalOnBean(ConsolePropertySourceLocator.class)
    public ConsoleConfigProperties consoleConfigProperties() {
        return new ConsoleConfigProperties();
    }

    @Bean
    @ConditionalOnBean(ConsolePropertySourceLocator.class)
    public ConsolePushConfigListenner consolePushConfigListenner() {
        return new ConsolePushConfigListenner();
    }

}
